package com.han.demo.transaction;

import com.han.demo.entity.User;
import com.han.demo.mapper.UserMapper;
import com.han.demo.utils.ExecutorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author hanjl
 * @Description Spring默认的事务传播类型,如果当前没有事务，则自己新建一个事务，如果当前存在事务，则加入这个事务
 * @ClassName Required
 * @createTime 2021/11/18 16:10
 */
@Service
public class Required {


    @Resource
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    public void testMain(){
        User a = new User();
        a.setName("required");
        A(a);
        testB();
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public void testB(){
        User b = new User();
        b.setName("required2");
        B(b);
        if(true){
            throw new NullPointerException();
        }
        User b2 = new User();
        b2.setName("required3");
        B(b2);
    }

    public void A(User user){
        userMapper.insert(user);
    }

    public void B(User user){
        userMapper.insert(user);
    }
}
